package trading_strategy.instruments;

import trading_strategy.Entity;
import trading_strategy.events.EventProvider;
import trading_strategy.markets.Market;

/**
 * Created by IntelliJ IDEA.
 * User: gderoujoux
 * Date: 6 mai 2010
 * Time: 10:49:57
 * To change this template use File | Settings | File Templates.
 */
public class ListedInstrument extends Entity {
    Integer marketId;
    final double multiplier;        
    transient Market market;
    final double tick;

    final EventProvider marketTradeProvider;
    final EventProvider orderBookProvider;

    public ListedInstrument(Integer id, Integer marketId, double multiplier, double tick) {
        super(id);
        this.multiplier = multiplier;
        this.tick = tick;
        this.marketId = marketId;
        marketTradeProvider = new EventProvider(getId() + "-trades", marketId + "-market");
        orderBookProvider = new EventProvider(getId() + "-orderbook", marketId + "-market");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ListedInstrument)) return false;

        ListedInstrument that = (ListedInstrument) o;

        return !(getId() != null ? !getId().equals(that.getId()) : that.getId() != null) && !(marketId != null ? !marketId.equals(that.marketId) : that.marketId != null);
    }

    @Override
    public int hashCode() {
        int result = getId() != null ? getId().hashCode() : 0;
        result = 31 * result + (marketId != null ? marketId.hashCode() : 0);
        return result;
    }

    public double getTick() {
        return tick;
    }

    public EventProvider getTradeEventProvider() {
        return marketTradeProvider;
    }

    public EventProvider getOrderBookProvider() {
        return orderBookProvider;
    }

    public Market getMarket() {
        if (market == null) {
            market = Market.getMarket(marketId);
        }
        return market;
    }

    public double getMultiplier() {
        return multiplier;
    }
}
